<?php
include_once(Utils::getPath(array('repositories', 'userRepository.php')));
include_once(Utils::getPath(array('summary.php')));


$userRep = new UserRepository();
$user = $userRep->getById($_SESSION['authenticatedId']);
$friendsCount = $userRep->countFriends($user->getId(), true);
if ($user == null) {
    $referer = getenv("HTTP_REFERER");
    header("Location: $referer");

    exit();
}

$avatar = $user->getImage();

$news = Summary::getSummaryInfo($user->getId());
?>
<div id="welcome">Welcome <?php echo $_SESSION['authenticatedUserName'] ?>&#33;</div>
<?php if (isset($avatar) && $avatar != null) { ?>
    <div id="avatar">
        <img alt="User avatar" src="<?php echo $avatar; ?>" />
    </div>
    <div id="shorcuts">
        <h4>Your shortcuts</h4>
        <ul>
            <li><a href="<?php echo Utils::getUrl('user/edit.php') ?>">Edit your profile</a></li>
            <li><a href="<?php echo Utils::getUrl('challenge/new.php') ?>">Upload challenge</a></li>
        </ul>
    </div>
<?php } ?>
<div id="summary">
    <h4>Your news</h4>
<?php for ($i = 0; $i < count($news); $i++) { ?>
        <div class="summary-element">
            <div class="summary-text"><?php echo $news[$i]['message'] ?></div>
            <?php if ($news[$i]['url'] != null) { ?><div class="summary-url"><a href="<?php echo $news[$i]['url'] ?>">View</a></div><?php } ?>
        </div>
    <?php
}
if (count($news) == 0) {
    ?>
        <div>There is nothing new to show.</div>
        <?php if ($friendsCount == 0) { ?>
        <div>You don't have any friend, you can search them in 'People' tab in top-menu.</div>
        <?php }
    } ?>
</div>